package com.niit.controller;

import com.niit.entity.Notification;
import com.niit.entity.User;
import com.niit.service.NotificationService;
import com.niit.utils.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

public class OldNotificationController {

    private NotificationService notificationService;

    @GetMapping("/view/{id}")
    public String getNotificationView(@PathVariable Integer id, Model model, HttpSession session) {
        Object account = session.getAttribute("account");
        if (account instanceof User user) {
            notificationService.markAsRead(user.getId(), id);
        }

        Notification notification = notificationService.getNotificationById(id);
        if (notification != null) {
            model.addAttribute("notification", notification);
            return "notification/notification-view";
        } else {
            return "redirect:/user/notification";
        }
    }

    @GetMapping("/getAll")
    public Result<?> getNotifications(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Byte level,
            @RequestParam(required = false) Byte type,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpSession session) {
        try {
            //Integer loginType = (Integer) session.getAttribute("loginType");
            Integer loginType = 3;
            Page<Notification> notifications = notificationService.getNotifications(
                    title, level, type, PageRequest.of(page, size), loginType);
            return Result.successPage(notifications.getContent(), notifications.getTotalElements()).code(200);
        } catch (Exception e) {
            return Result.error("获取通知列表失败");
        }
    }

    @GetMapping("/findAll")
    public List<Notification> findNotifications(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Byte level,
            @RequestParam(required = false) Byte type,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpSession session) {
        //Integer loginType = (Integer) session.getAttribute("loginType");
        Integer loginType = 3;
        Page<Notification> notifications = notificationService.getNotifications(title, level, type, PageRequest.of(page, size), loginType);
        return notifications.getContent();
    }

    @PostMapping
    public Result<?> createNotification(@RequestBody Notification notification, HttpSession session) {
        try {

            notification.setCreateTime(LocalDateTime.now());
            notificationService.createNotification(notification);
            return Result.success(notification);
        } catch (Exception e) {
            return Result.error("创建通知失败");
        }
    }

    @PutMapping("/{id}")
    public Result<?> updateNotification(
            @PathVariable Integer id,
            @RequestBody Notification notification,
            HttpSession session) {
        try {
            notificationService.updateNotification(id, notification);
            return Result.success(notification);
        } catch (Exception e) {
            return Result.error("更新通知失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteNotification(@PathVariable Integer id, HttpSession session) {
        try {
            notificationService.deleteNotification(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除通知失败");
        }
    }

    @DeleteMapping("/batch")
    public Result<?> deleteNotifications(@RequestBody List<Integer> ids, HttpSession session) {
        try {
            notificationService.deleteNotifications(ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error("批量删除通知失败");
        }
    }

    @PostMapping("/read/{notificationId}")
    public Result<?> markAsRead(
            @PathVariable Integer notificationId,
            HttpSession session) {
        try {
            Object account = session.getAttribute("account");
            if (account instanceof User user) {
                notificationService.markAsRead(user.getId(), notificationId);
                return Result.success();
            }
            return Result.error(401, "未登录");
        } catch (Exception e) {
            return Result.error("标记已读失败");
        }
    }
}